//
//  NWStatusViewModel.swift
//  Weibo
//
//  Created by Northwan on 16/1/24.
//  Copyright © 2016年 Northwan. All rights reserved.
//

import UIKit

class NWStatusViewModel: NSObject {
    var status:NWStatus?
    
    //获取头像地址
    var headImageStr:String?
    //获取vip图片
    var vipImage:UIImage?
    //认证图片
    var verifiedImage:UIImage?
    //转发评论赞
    var repostsCountString:String = "转发"
    var commentsCountString:String = "评论"
    var attitudesCountString:String = "赞"
    //转发微博内容
    var retweetText: String?
    //来源
    var sourceText: String?
    //创建时间
    var createAtText:String{
        if let createText = status?.created_at{
            guard let createDate = NSDate.sinaDate(createText) else{
                return ""
            }
            return createDate.singDateString
        }
        
        return ""
    }
    
    init(status:NWStatus) {
        super.init()
        self.status = status
        
        setStatus()
    }
    
    /**
     因为内部设置数据不会调用didSet（必须是构造函数内设置不会调用）,所以只能在设置完数据后统一初始化一次，这样就避免重复计算，节省cpu
     */
    private func setStatus(){
        
        // head
        if let urlString = status?.user?.profile_image_url{
            headImageStr = urlString
        }
        
        //vip
        if let mbrank = status?.user?.mbrank {
            if mbrank > 0 && mbrank < 7{
                   vipImage =  UIImage(named: "common_icon_membership_level\(mbrank)")
            }
        }
        
        //认证图片
        if let verified = status?.user?.verified {
            switch verified {
            case 1:
                verifiedImage = UIImage(named: "avatar_vip")
            case 2,3,5:
                verifiedImage = UIImage(named: "avatar_enterprise_vip")
            case 220:
                verifiedImage = UIImage(named: "avatar_grassroot")
            default:
                verifiedImage = nil
            }
         }
        
        //转发微博内容
        if let retweetStatus =  status?.retweeted_status {
            if let name = retweetStatus.user?.name{
                retweetText  = "@\(name):\(retweetStatus.text!)"
            }
         }
        
        //来源处理
        if let sourceString = status?.source where sourceString.containsString("\">") {
           let startRange = sourceString.rangeOfString("\">")!
           let endRange = sourceString.rangeOfString("</")!
           let result = sourceString.substringWithRange(startRange.endIndex..<endRange.startIndex)
            sourceText = "来自\(result)"
        }
        
        //如果小于10000，显示具体数字，如果大于10000并且小于11000，显示1万，大于11000显示1.1
        repostsCountString = getCountString(self.status?.reposts_count ?? 0, defaultTitle: "转发")
        commentsCountString = getCountString(self.status?.comments_count ?? 0, defaultTitle: "评论")
        attitudesCountString = getCountString(self.status?.attitudes_count ?? 0, defaultTitle: "赞")
    }
    
    /**
     计算数量的的显示方式
     
     - parameter count:        数量
     - parameter defaultTitle: 默认文字
     
     - returns:
     */
    private func getCountString(count:Int,defaultTitle:String)->String{
        if count > 0 {
            if count < 10000{
                return "\(count)"
            }else{
                let result =  CGFloat(count/10000)/10
                let resultStr = "\(result)万"
                if resultStr.containsString(".0"){
                    return resultStr.stringByReplacingOccurrencesOfString(".0", withString: "")
                }
                return resultStr
            }
        }
        return defaultTitle
    }
    
}
